from sqlalchemy.orm import Session
from . import models, schemas

def get_profile_field(db: Session, profile_field_id: int):
    return db.query(models.ProfileFields).filter(models.ProfileFields.Id == profile_field_id).first()

def get_profile_fields(db: Session, skip: int = 0, limit: int = 100):
    return db.query(models.ProfileFields).offset(skip).limit(limit).all()

def create_profile_field(db: Session, profile_field: schemas.ProfileFieldsCreate):
    db_profile_field = models.ProfileFields(**profile_field.dict())
    db.add(db_profile_field)
    db.commit()
    db.refresh(db_profile_field)
    return db_profile_field

def update_profile_field(db: Session, profile_field_id: int, profile_field: schemas.ProfileFieldsCreate):
    db_profile_field = db.query(models.ProfileFields).filter(models.ProfileFields.Id == profile_field_id).first()
    if db_profile_field:
        for key, value in profile_field.dict().items():
            setattr(db_profile_field, key, value)
        db.commit()
        db.refresh(db_profile_field)
    return db_profile_field

def delete_profile_field(db: Session, profile_field_id: int):
    db_profile_field = db.query(models.ProfileFields).filter(models.ProfileFields.Id == profile_field_id).first()
    if db_profile_field:
        db.delete(db_profile_field)
        db.commit()
    return db_profile_field